//
//  CategoryListViewController.m
//  FlyGift
//
//  Created by lanou3g on 15/7/16.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "CategoryListViewController.h"
#import "CategoryListCollectionViewCell.h"
#define kCategoryListURL @"http://api.liwushuo.com/v1/item_subcategories/%@/items?limit=20&offset=0"
#import "CategoryListModel.h"
#import "CategoryListWaterflowLayout.h"
#import "GiftDetailViewController.h"
#import "CategoryListShowCell.h"

@interface CategoryListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,CategoryListWaterflowLayoutDelegate>
@property(nonatomic, strong)UICollectionView *categoryListCollectionView;
@property(nonatomic, strong)NSMutableArray *categoryListModelArray;
@end

@implementation CategoryListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.titleName;
    [self loadCollectionView];
}


- (void)loadCollectionView
{
    CategoryListWaterflowLayout *flowlayout = [[CategoryListWaterflowLayout alloc] init];
    flowlayout.delegate = self;
    self.categoryListCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) collectionViewLayout:flowlayout];
    self.categoryListCollectionView.delegate = self;
    self.categoryListCollectionView.dataSource = self;
    self.categoryListCollectionView.backgroundColor = KColor(207, 207, 207, 1);
    [self.view addSubview:_categoryListCollectionView];
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.categoryListModelArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item == 0)
    {
        static NSString *cell1 = @"cell00";
        [collectionView registerClass:[CategoryListShowCell class] forCellWithReuseIdentifier:cell1];
        CategoryListShowCell *showCell = [collectionView dequeueReusableCellWithReuseIdentifier:cell1 forIndexPath:indexPath];
        showCell.backgroundColor = [UIColor whiteColor];
        return showCell;
    }
    else
    {
        static NSString *catelistcell = @"CategoryListCell";
        [collectionView registerClass:[CategoryListCollectionViewCell class] forCellWithReuseIdentifier:catelistcell];
        CategoryListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:catelistcell forIndexPath:indexPath];
        cell.backgroundColor = [UIColor whiteColor];
        [cell.heartButton addTarget:self action:@selector(heartButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.categoryListModel = self.categoryListModelArray[indexPath.item - 1];
        if (!cell.categoryListModel.isSelect)
        {
            [cell.heartButton  setBackgroundImage:[UIImage imageNamed:@"heart_w@3x"] forState:UIControlStateNormal];
        }
        else
        {
            [cell.heartButton setBackgroundImage:[UIImage imageNamed:@"icon_bbs_like_on_bottom@3x"] forState:UIControlStateNormal];
        }
        return cell;
    }
}

#pragma mark - UICollectionViewDelegate
#pragma mark - 点击跳转到礼物详情页面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.item != 0)
    {
        CategoryListModel *model = self.categoryListModelArray[indexPath.item - 1];
        NSLog(@"点击了第%ld的Item",indexPath.item);
        GiftDetailViewController *giftDetailVC = [[GiftDetailViewController alloc]init];
        giftDetailVC.giftDetailUrl = model.purchase_url;
        [self.navigationController pushViewController:giftDetailVC animated:YES];
    }

}

#pragma mark - CategoryListWaterflowLayoutDelegate
- (CGFloat)categoryListWaterflowLayout:(CategoryListWaterflowLayout *)waterflowLayout heightForWidth:(CGFloat)width atIndexpath:(NSIndexPath *)indexPath
{
//    CategoryListModel *model = self.categoryListModelArray[indexPath.item];
//    __block CGFloat w;
//    __block CGFloat h;
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//        // 放在子线程 否则会阻塞主线程
//       UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:model.cover_image_url]]];
//        w = image.size.width;
//        h = image.size.height;
//    });
    
//    return image.size.height / image.size.width * width + kP6Height(20);
//    return model.h / model.w * width + kP6Height(20);
    
    if (indexPath.item == 0) {
        return 55;
    }else
    {
        return width + kP6Height(45);
    }


}

#pragma mark - 数据解析
- (void)categoryListDataRequestWithID:(NSString *)ID
{
    NSString *url = [NSString stringWithFormat:kCategoryListURL,ID];
    NSLog(@"===========%@",url);
    [LORequestManger GET:url success:^(id response) {
        if (response == nil)
        {
            return;
        }
        
        NSDictionary *dictionary = (NSDictionary *)response;
        NSDictionary *dataDic = dictionary[@"data"];
        NSArray *array = dataDic[@"items"];
        self.categoryListModelArray = [CategoryListModel jsonToCategoryListModelArray:array];
        [self.categoryListCollectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"categoryListCollection==ERROR:%@",error);
    }];
}


#pragma mark - ❤️点击事件
- (void)heartButtonAction:(UIButton *)button
{
    NSLog(@"点击了按钮");
    CategoryListCollectionViewCell *cell = (CategoryListCollectionViewCell *)button.superview;
    NSIndexPath *indexPath = [self.categoryListCollectionView indexPathForCell:cell];
    CategoryListModel *model = self.categoryListModelArray[indexPath.item - 1];
    if (!model.isSelect)
    {
        [self heartAnimation:button];
        [button setBackgroundImage:[UIImage imageNamed:@"icon_bbs_like_on_bottom@3x"] forState:UIControlStateNormal];
        [MBProgressHUD showSuccess:@"已加入心愿单"];
    
        MBProgressHUD *mb = [[MBProgressHUD alloc] init];
        mb.color = kMainColor;
    }
    else
    {
        [button setBackgroundImage:[UIImage imageNamed:@"heart_w@3x"] forState:UIControlStateNormal];
    }
    model.isSelect = !model.isSelect;
}

#pragma mark - ❤️动画
- (void)heartAnimation:(UIButton *)button
{
    CategoryListCollectionViewCell *cell = (CategoryListCollectionViewCell *)button.superview;
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_bbs_like_on_bottom@3x"]];
    imageView.frame = CGRectMake((cell.frame.size.width - 40) / 2, (cell.frame.size.height - 40) / 2, 40, 40);
    [cell addSubview:imageView];
    [UIView animateWithDuration:1.0
                     animations:^{
                         imageView.transform = CGAffineTransformMakeScale(2, 2);
                         imageView.alpha = 1.0;
                     }
                     completion:^(BOOL finished) {
                         [UIView animateWithDuration:1.0
                                          animations:^{
                                              imageView.transform = CGAffineTransformMakeScale(1, 1);
                                              imageView.alpha = 0.0;
                                          }
                                          completion:^(BOOL finished) {
                                              [imageView removeFromSuperview];
                                          }];
                     }];
    
}

@end











